<%--
  Created by IntelliJ IDEA.
  User: peizangpin
  Date: 2017/8/7
  Time: 下午11:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="../commons-easyui.jsp"%>
<html>
<head>
    <title>权限管理页面</title>
    <style type="text/css">
        #authForm > div{
            margin: 10px 0px;
        }
        #authForm > div > .easyui-validatebox{
            height:25px;
        }
    </style>

    <script type="text/javascript" src="<%=basePath%>common/script/customize/customize-old.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            initAuthTable();
        });

        function initAuthTable(){
            $('#authTreeTable').treegrid({
                url:basePath+'authAction/getTreeAuth.json'
                ,method:'post'
                ,idField:'authId'
                ,treeField:'authName'
                ,border:false
//                ,toolbar:'#authTreeToolbar'
                ,columns:[[
                    {field:'authName',title:'权限名称'}
                    ,{field:'authTotalCode',title:'权限绝对路径'}
                    ,{field:'level',title:'权限等级'}
                ]]
                ,fit:true
                ,loadFilter:function (data) {
                    $.each(data.rows,function (index,row) {
                        if(row.authType == 0 && row.hasChildren == 0){
                            data.rows[index].state='closed';
                        }
                    });
                    return data;
                }
                ,onBeforeLoad:function (node,param) {
                    if(!isBlank(node)){
                        return false;
                    }
                }
            });
        }

        function beforeOpenAuthDialog(openMode){
            var selectAuth = getSelectAuth('self');
            if(isBlank(selectAuth)){
                $.messager.alert('警告','没有选择权限！','warning',emptyFn());
                return false;
            }
            if(isBlank(openMode)){return false;}
            $('#authForm').form('clear');
            $('#addMode').combobox('select',1);
            $('#addMode').combobox('enable');
            if(openMode == 'add'){
                $('#authForm').data('submitMode','add');
                $('#addModeDiv').css('display','block');
                $('#authCodeDiv').css('display','block');
                var selectAuth = getSelectAuth('self');
                if(selectAuth.authId == 0){
                    $('#addMode').combobox('disable');
                }

            }else if(openMode == 'edit'){
                $('#authForm').data('submitMode','edit');
                $('#addModeDiv').css('display','none');
                $('#authCodeDiv').css('display','none');

                $('#authId').val(selectAuth.authId);
                $('#authCode').val(selectAuth.authCode);
                $('#authName').val(selectAuth.authName);
                $('input[name="authType"][value="'+selectAuth.authType+'"]').prop('checked',true);
                $('input[name="authType"][value="'+selectAuth.authType+'"]').trigger('click');

                if(selectAuth.authType == 1){
                    $('#level').val(selectAuth.level);
                }
            }
        }

        function getSelectAuth(mode){
            if(mode == 'self'){
                 return $('#authTreeTable').treegrid('getSelected');
            }else if(mode == 'parent'){
                var select = getSelectAuth('self');
                if($.isEmptyObject(select)){
                    return null;
                }
                return $('#authTreeTable').treegrid('getParent',select.authId);
            }
        }
        function reloadAuth(){
            $('#authTreeTable').treegrid('reload');
        }

        function submitAuthForm() {
            var select = getSelectAuth('self');
            var mode = $('#authForm').data('submitMode');
            var addMode = $("#addMode").combobox('getValue');
            var param = getJSONFormData('authForm');

            if(!validateForm()){
                return;
            }

            param.parentId = select.authId;
            if(addMode == 0){ //添加同辈
                param.parentId = select.parentId;
            }else if(addMode != 1){
                $.messager.alert('添加失败','添加模式错误！','error',emptyFn());
                return ;
            }

            if(mode == 'add'){
                tmsAjax({
                    url:basePath+'authAction/saveAuth.json'
                    ,type:'post'
                    ,data:JSON.stringify(param)
                    ,success:function(data){
                        if(!isBlank(data)){
                            easyuiAlert('添加成功','info');
                            closeDialog('authDialog',reloadAuth);
                        }

                    }
                });
            }if(mode == 'edit'){
                tmsAjax({
                    url:basePath+'authAction/updateAuth.json'
                    ,type:'post'
                    ,data:JSON.stringify(param)
                    ,isResultState:true
                    ,success:function(data){
                        closeDialog('authDialog',reloadAuth);
                    }
                });
            }
        }

        function validateForm(){
            var param = getJSONFormData('authForm');
            if(isBlank(param.authCode)){
                easyuiAlert('权限编码无法为空!','warning');
                return false;
            }

            if(isBlank(param.authName)){
                easyuiAlert('权限名称无法为空!','warning');
                return false;
            }

            if(isBlank(param.authType)){
                easyuiAlert('请选择权限类别!','warning');
                return false;
            }

            if($('#levelDiv').css('display') != 'none'){
                if(isBlank(param.level)){
                    easyuiAlert('请选择权限等级!','warning');
                    return false;
                }
            }

            return true;
        }

        function deleteAuth(){
            var select = getSelectAuth('self');
            if(isBlank(select)){easyuiAlert("没有选择权限!","error");}
            $.messager.confirm("确认对话框","确定删除权限"+select.authTotalCode+"？",function (confirm) {
                if(confirm){
                    tmsAjax({
                        url:basePath+"authAction/deleteAuth.json?authId="+select.authId
                        ,type:'post'
                        ,dataType:'json'
                        ,isResultState:true
                    });
                }
            });
        }

        function showLevel(flag){
            if(!flag){
                $('#levelDiv').css('display','none');
            }else{
                $('#levelDiv').css('display','block');
            }

        }
    </script>
</head>
<body>
    <table id = "authTreeTable">
    </table>

    <div id = "authTreeToolbar">
        <a class="easyui-linkbutton" data-options="iconCls:'icon-add'" onclick="openOrInitDialog('authDialog','添加权限',null,null,beforeOpenAuthDialog,'add')">添加权限</a>
        <a class="easyui-linkbutton" data-options="iconCls:'icon-edit'" onclick="openOrInitDialog('authDialog','修改权限',null,null,beforeOpenAuthDialog,'edit')">修改权限</a>
        <a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" onclick="deleteAuth()">删除权限</a>
    </div>

    <div id="authDialog" style="display: none;padding: 10px;">
        <form id="authForm">
            <input type="hidden" name="authId" id="authId" />
            <div id="authCodeDiv">
                <label>权限编码：</label>
                <input id="authCode" name = "authCode" class="easyui-validatebox textbox">
            </div>
            <div>
                <label>权限名称：</label>
                <input id="authName" name = "authName" class="easyui-validatebox textbox">
            </div>
            <div>
                <label>权限类型：</label>
                <input type="radio" name ="authType" value="0" onclick="showLevel(false)">权限文件夹
                <input type="radio" name ="authType" value="1" onclick="showLevel(true)">功能权限
            </div>
            <div id="levelDiv">
                <label>权限等级：</label>
                <select id="level" name="level">
                    <option value="0">0</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                </select>
            </div>
        </form>
        <div id="addModeDiv" style="margin: 10px 0px;height: 25px;">
            <select id="addMode" name="addMode" class="easyui-combobox" data-options="fit:true,editable:false" >
                <option value="1" selected >添加子项</option>
                <option value="0">添加同辈</option>
            </select>
        </div>
        <div style="height: 25px;margin: 10px 0px;">
            <a class="easyui-linkbutton" data-options="iconCls:'icon-ok',fit:true" onclick="submitAuthForm()">提交</a>
            <!-- <a class="easyui-linkbutton" data-options="iconCls:'icon-ok',fit:true" onclick="validateForm()">校验</a> -->
        </div>
    </div>
</body>
</html>
